package net.dl.ss.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.dl.ss.dao.CoupleDao;
import net.dl.ss.domain.Adjudicator;
import net.dl.ss.domain.Couple;
import net.dl.ss.service.ContextManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author yakovlev.yuriy@gmail.com
 * @since 12.8.2008
 */
@Controller
public class CouplesController extends BaseController {
	protected final Log logger = LogFactory.getLog(getClass());
	private final CoupleDao coupleDao;
	
	@Autowired
	public CouplesController(CoupleDao coupleDao) {
		this.coupleDao = coupleDao;
	}
	
	@RequestMapping("/couplesList.htm")
	protected void couplesListHandler(HttpServletRequest request, HttpServletResponse response) {
		try {
			ServletOutputStream  os = response.getOutputStream();
			modifyResponseHeader(response, "text/xml");
			
		    os.println("<couples>");
		    Long eventId = ContextManager.getCurrentEvent(request);
		    if(eventId!=null) {
		    	List<Couple> couples = this.coupleDao.findByEventId(eventId);
		    	for(Couple couple : couples) {
		    		os.print(couple.toXML());
		    	}
		    }
		    os.println("</couples>");
	
			os.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		} 
	}
	
	@RequestMapping("/categCouplesList.htm")
	protected void categCouplesListHandler(HttpServletRequest request, HttpServletResponse response) {
		try {
			ServletOutputStream  os = response.getOutputStream();
			modifyResponseHeader(response, "text/xml");
			
		    os.println("<couples>");
		    Long categoryId = ContextManager.getCurrentCategory(request);
		    if(categoryId!=null) {
		    	List<Couple> couples = this.coupleDao.findByCategoryId(categoryId);
		    	for(Couple couple : couples) {
		    		os.print(couple.toXML());
		    	}
		    }	
		    os.println("</couples>");
	
			os.close();
		} catch (IOException e) {
			logger.error(e.getMessage());
		} 
	}
	
	@RequestMapping("/coupleUpdate.htm")
	public void coupleUpdateHandler(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream  os = response.getOutputStream();
		modifyResponseHeader(response, "text/plain");
		
		Long eventId = ContextManager.getCurrentEvent(request);
	    if(eventId!=null) {
			Couple couple = new Couple();
			couple.setEventId(eventId);
			couple.setNumber(request.getParameter("number"));
			couple.setName(request.getParameter("name"));
			couple.setLevel(request.getParameter("level"));
			couple.setCity(request.getParameter("city"));
			couple.setClub(request.getParameter("club"));
			couple.setTrainer(request.getParameter("trainer"));
			try {
				String id = request.getParameter("id");
				if(id!=null && !"".equals(id)) {
					couple.setId(Long.parseLong(id));
					this.coupleDao.updateEntity(couple);
				} else {
					this.coupleDao.saveEntity(couple);
				}
				os.print("{success:true}");
			} catch(Exception ex) {
				logger.error(ex.getMessage());
			}
	    } else {
	    	os.print("{success:false}");
	    }
	    os.close();
	}
	
	@RequestMapping("/coupleDelete.htm")
	public void coupleDeleteHandler(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream  os = response.getOutputStream();
		modifyResponseHeader(response, "text/plain");
		try {
			String id = request.getParameter("id");
			if(id!=null && !"".equals(id)) {
				this.coupleDao.deleteEntityById(Long.parseLong(id));
				os.print("{success:true}");
			} else {
				os.print("{success:false}");
			}
		} catch(Exception ex) {
			logger.error(ex.getMessage());
		}
		os.close();
	}

}
